package com.captainbank.tcl.tclapplication.ui.views;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.utils.MyToast;
import com.captainbank.tcl.tclapplication.utils.StringUtil;
import com.gyf.barlibrary.ImmersionBar;

/**
 * Created by TCL on 2019/4/4.
 * Describe:
 */
public class LoginDialogFragment extends BaseDialogFragment {
    private EditText et_name, et_psw;
    private Button btn_login;
    private String showInfo;

    public static LoginDialogFragment newInstance(String mShowInfo) {
        LoginDialogFragment fragment = new LoginDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("info", mShowInfo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_login_dialog_fragment;
    }

    @Override
    protected void initData(Bundle bundle) {
        if(bundle!=null){
            showInfo = bundle.getString("info","");
        }
    }

    @Override
    protected void initView(View v) {
            et_name = v.findViewById(R.id.et_name);
            et_psw = v.findViewById(R.id.et_psw);
            btn_login = v.findViewById(R.id.btn_login);
            btn_login.setText(showInfo);
            Button btn_login = v.findViewById(R.id.btn_login);
            btn_login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String name = et_name.getText().toString().trim();
                    String psw = et_psw.getText().toString().trim();
                    if (!StringUtil.checkIsNotNull(name)) {
                        MyToast.toastError(getActivity(), "请输入用户名", false);
                    } else if (!StringUtil.checkIsNotNull(psw)) {
                        MyToast.toastError(getActivity(), "请输入密码", false);
                    } else {
                        LoginInputListener listener = (LoginInputListener) getActivity();
                        if (listener != null) {
                            listener.onLoginInputComplete(name, psw);
                        }
                        dismiss();
                    }
                }
            });
    }

    @Override
    protected int setLeftRightPadding() {
        return 32;
    }

    @Override
    protected int setGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    protected void initImmersionBar() {
        ImmersionBar.with(this).navigationBarColor(R.color.colorPrimary).init();
    }

    //输入账号密码
    public interface LoginInputListener {
        void onLoginInputComplete(String username, String password);
    }

}
